package b1.mobile.blackberry.screen;

import java.util.Vector;

import b1.mobile.blackberry.dao.DataAccessDelegate;
import b1.mobile.blackberry.dao.ReportDAO;
import b1.mobile.blackberry.entity.ReportEntity;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.lang.CommonConstant;
import b1.mobile.blackberry.ui.component.IconListField;
import b1.mobile.blackberry.ui.component.util.Capsule;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;
import b1.mobile.blackberry.util.OpenFileUtil;
import b1.mobile.blackberry.util.PDFDownloadUtil;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class ReportListScreen extends BaseFunctionScreen implements DataAccessDelegate{

	private static ResourceBundle resources = ResourceBundle.getBundle(
			BUNDLE_ID, BUNDLE_NAME);
	
	private IconListField _listField;
	Manager bgMgr = new VerticalFieldManager(Field.USE_ALL_WIDTH);

	/**
	 * 
	 */
	public ReportListScreen() {
		setTitle(FieldFactory.makeScreenTitle(resources.getString(TITLE_REPORT_LIST)));
		ThemeUtil.decorateScreen(this);

		bgMgr.setMargin(10, 0, 10, 0);
		bgMgr.setBackground(BackgroundFactory.createSolidBackground(0x00f3f3f3));
		
		_listField = new IconListField();
		_listField.setListType(CommonConstant.ICON_LIST_TYPE_REPORT_LIST);
		_listField.setRowHeight(_listField.getRowHeight()*2);	
		
		add(bgMgr);
		bgMgr.add(_listField);
	}
	
	protected void onUiEngineAttached(boolean attached) {
		if (attached){
			ReportDAO dao = new ReportDAO(this);
			dao.list();
		}
	}
	
	protected void makeMenu(Menu menu, int instance) {
		
	}

	public void onDataReady(Serviceworker worker, Object data) {
		super.onDataReady(worker, data);
		if(worker.responseCode == 200){
			if(worker.getOperator().equals(ReportDAO.OP_LIST)){
				Vector vData = (Vector)data;
				final Capsule[] items = new Capsule[vData.size()];
				vData.copyInto(items);
				
				UiApplication.getUiApplication().invokeLater(new Runnable(){
					public void run(){
						_listField.set(items);
					}
				});
			} else {
				/**
				 * invoke browser to open report url
				 */
				PDFDownloadUtil.DownloadFile((String)data, "file:///store/home/user/temp.pdf");
//				OpenFileUtil.openReport((String)data);
			}			
		}
	}
	
	protected boolean navigationClick(int status, int time) {
		if (getLeafFieldWithFocus() instanceof IconListField){
			Capsule capsule = (Capsule) _listField.get(_listField, _listField.getSelectedIndex());
			Object value = capsule.value ;
			if (value instanceof ReportEntity){
				ReportEntity ety = (ReportEntity)value;
				ReportDAO dao = new ReportDAO(this);
				dao.get(ety.getCode());
//				getUiEngine().pushScreen(new BPInfoScreen(((BPEntity)value).getNameId()));
			}
		} else{
			super.navigationClick(status, time);
		}
		return true;
	}
	

}
